﻿using AutoMapper;
using PowerHub.MES.Common.NumberingCenter;
using PowerHub.MES.DTO.Crew.DTO;
using PowerHub.MES.DTO.Crew.QueryDto;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.Crew;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.ICrew;
using PowerHub.MES.IServices.ICrew;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.DTO.ProductPlanning.DTO;
using PowerHub.MES.Entities.ProductPlanning;
using PowerHub.MES.DTO.ProductPlanning.QueryDTO;
using PowerHub.MES.IServices.IProductPlanning;
using PowerHub.MES.IRepository.IProductPlanning;
using PowerHub.MES.DTO.BasicData.DTO;
using PowerHub.MES.Entities.BasicData;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace PowerHub.MES.Services.ProductPlanning
{

    /// <summary>
    /// 原料分类接口实现层
    /// </summary>
    public class ProductPlanning_ProductClassifyServices : BaseServices<ProductPlanning_ProductClassify>, IProductPlanning_ProductClassifyServices
    {
        private IProductPlanning_ProductClassifyRepository _iProductPlanning_ProductClassifyRepository;
        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public ProductPlanning_ProductClassifyServices(PowerHubMesContext powerHubMesContext, IProductPlanning_ProductClassifyRepository iProductPlanning_ProductClassifyRepository, UserServices userServices, IMapper mapper) : base(powerHubMesContext)
        {
            _iProductPlanning_ProductClassifyRepository = iProductPlanning_ProductClassifyRepository;
            _userServices = userServices;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取原料分类列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<ProductPlanning_ProductClassifyDTOs>>> GetProductPlanning_ProductClassifyPageListAsync(QueryProductPlanning_ProductClassifyDTO query)
        {
            var result = new OperationPageResult<List<ProductPlanning_ProductClassifyDTOs>>();
            var ProductClassifyRepositorys = await _iProductPlanning_ProductClassifyRepository.GetProductPlanning_ProductClassifyPageListAsync(query);
            result.PageIndex = ProductClassifyRepositorys.PageIndex;
            result.PageSize = ProductClassifyRepositorys.PageSize;
            result.TotalCount = ProductClassifyRepositorys.TotalCount;


            //取出一级菜单
            List<ProductPlanning_ProductClassifyDTOs> menuInfoList = ProductClassifyRepositorys.Data.Where(m => m.ParentId == null || m.ParentId == "").Select(m => new ProductPlanning_ProductClassifyDTOs(m)).OrderBy(m => m.ProcessSort).ToList();

            GetChild(menuInfoList, ProductClassifyRepositorys.Data);
            result.Data = menuInfoList;
        
            return result;
        }

        /// <summary>
        /// 递归获取子菜单项
        /// </summary>
        /// <param name="menuInfoList"></param>
        /// <param name="menuInfosss"></param>
        private void GetChild(List<ProductPlanning_ProductClassifyDTOs> menuInfoList, List<ProductPlanning_ProductClassify> menuInfosss)
        {
            foreach (var item in menuInfoList)
            {
                var children = menuInfosss.Where(x => x.ParentId == item.Id).Select(m => new ProductPlanning_ProductClassifyDTOs(m)).OrderBy(d => d.ProcessSort).ToList();
                // 递归获取子菜单项的子菜单项
                GetChild(children, menuInfosss);
                item.Children = children.Count > 0 ? children : null;
            }
        }

        /// <summary>
        /// 添加原料分类
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateProductPlanning_ProductClassifyAsync(ProductPlanning_ProductClassifyDTO query)
        {
            ProductPlanning_ProductClassify foundation = await GetEntityAsync(d => d.ProductClassifyName.Equals(query.ProductClassifyName) && d.IsDel == 0);
            if (foundation != null)
            {
                throw new Exception("你输入的原料分类名称已存在！");
            }
            //实体转换
            var ProductPlanning_ProductClassify = _mapper.Map<ProductPlanning_ProductClassify>(query);
            ProductPlanning_ProductClassify.Id = Guid.NewGuid().ToString();
            ProductPlanning_ProductClassify.ProductClassifyNo = string.IsNullOrEmpty(query.ProductClassifyNo) || query.ProductClassifyNo.Contains(" ") ? GenerateBasicDataNO.GenerateCode(query.ProductClassifyName) : query.ProductClassifyNo;
            ProductPlanning_ProductClassify.Creator = _userServices.UserName;

            return await _iProductPlanning_ProductClassifyRepository.CreateProductPlanning_ProductClassifyAsync(ProductPlanning_ProductClassify);
        }


        /// <summary>
        /// 批量删除原料分类
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionProductPlanning_ProductClassifyAsync(List<string>? ids)
        {
            if (ids.Count < 1)
            {
                throw new Exception("请先选择数据！");
            }
            return await _iProductPlanning_ProductClassifyRepository.DeletionProductPlanning_ProductClassifyAsync(ids);
        }


        /// <summary>
        /// 更新原料分类
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateProductPlanning_ProductClassifyAsync(ProductPlanning_ProductClassifyDTO query)
        {
            if (query == null)
            {
                throw new Exception("请先选择数据！");
            }

            //根据id获取原料分类信息
            ProductPlanning_ProductClassify ProductPlanning_ProductClassify = await GetEntityAsync(d => d.Id.Equals(query.Id));
            if (ProductPlanning_ProductClassify == null)
            {
                throw new Exception("更新原料分类信息发生了异常,请刷新页面后试试");
            }
            ProductPlanning_ProductClassify.ProductClassifyName = query.ProductClassifyName;
            ProductPlanning_ProductClassify.Updator = _userServices.UserName;

            return await _iProductPlanning_ProductClassifyRepository.UpdateProductPlanning_ProductClassifyAsync(ProductPlanning_ProductClassify);

        }

        /// <summary>
        /// 获取原料分类列表(树形）
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<OperationResult<List<ProductPlanning_ProductClassifyDTOTree>>> GetProductPlanning_ProductClassifyTreeListAsync(QueryProductPlanning_ProductClassifyDTO pageQuery)
        {
            var result = new OperationResult<List<ProductPlanning_ProductClassifyDTOTree>>();
            var ProductClassifyRepositorys = await _iProductPlanning_ProductClassifyRepository.GetProductPlanning_ProductClassifyPageListAsync(pageQuery);

            //取出一级菜单
            List<ProductPlanning_ProductClassifyDTOTree> menuInfoList = ProductClassifyRepositorys.Data.Where(m => m.ParentId == null || m.ParentId == "").Select(m => new ProductPlanning_ProductClassifyDTOTree(m)).OrderBy(m => m.ProcessSort).ToList();

            GetChildTree(menuInfoList, ProductClassifyRepositorys.Data);
            result.Data = menuInfoList;

            return result;
        }

        private void GetChildTree(List<ProductPlanning_ProductClassifyDTOTree> menuInfoList, List<ProductPlanning_ProductClassify> menuInfosss)
        {
            foreach (var item in menuInfoList)
            {
                var children = menuInfosss.Where(x => x.ParentId == item.value).Select(m => new ProductPlanning_ProductClassifyDTOTree(m)).OrderBy(d => d.ProcessSort).ToList();
                // 递归获取子菜单项的子菜单项
                GetChildTree(children, menuInfosss);
                item.Children = children.Count > 0 ? children : null;
            }
        }
    }
}
